package ray;

import ray.math.Color;
import ray.math.Point3;
import ray.math.Vector3;

public class Spotlight extends Light {
    
    private Vector3 direction;
    public void setDirection(Vector3 direction) {
        direction.normalize();
        this.direction = direction;
    }
    
    private double beamAngle;
    public void setBeamAngle(double beamAngle) {
        this.beamAngle = Math.toRadians(beamAngle);
    }
    
    private double falloffAngle;
    public void setFalloffAngle(double falloffAngle) {
        this.falloffAngle = Math.toRadians(falloffAngle);
    }
    
    // empty construtor required for parser
    public Spotlight() { }
    
    public Color getIntensity(Point3 litSurfacePoint) {
        Vector3 toSurface = new Vector3();
        toSurface.sub(litSurfacePoint, position);
        toSurface.normalize();
        
        double angleBetweenDirectionSurface = Math.acos(toSurface.dot(direction));
        
        if (angleBetweenDirectionSurface > beamAngle + falloffAngle) {
            return new Color();
        }
        
        if (angleBetweenDirectionSurface > beamAngle) {
            Color result = new Color(intensity);
            double portion = (angleBetweenDirectionSurface - beamAngle) / falloffAngle;
            result.scale(1 - portion);
            return result;
        }
        
        return intensity;
    }
    
}
